# -*- coding:utf-8 -*-
# 图灵打码平台  https://tulingtech.xyz/#/
import json
import requests
import base64
import time, os
from PIL import Image        # Image专门处理图片的模块
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By


class LoginData(object):
    def __init__(self):
        self.url = "https://www.geetest.com/demo/click-float.html"
        self.driver = webdriver.Chrome()  # 创建浏览器对象

    def chrome_settings(self):
        # 打开浏览器指定页面
        self.driver.get(self.url)

        # 设置打开浏览器窗口长和宽
        self.driver.set_window_size(1100, 900)
        time.sleep(3)

        # 点击验证
        self.driver.find_element(By.XPATH, '//*[@id="captcha"]/div[3]/div[2]/div[1]/div[3]/span[1]').click()

    def save_image(self):
        # 用时间戳对图片命名
        image_name = str(int(time.time() * 1000)) + ".png"
        # 创建文件夹
        if not os.path.exists("Image_Big"):
            os.mkdir("Image_Big")
        if not os.path.exists("Image_Small"):
            os.mkdir("Image_Small")
        time.sleep(1)

        # 整个页面截图保存
        self.driver.save_screenshot("yzm.png")
        img = Image.open("yzm.png")    # 创建验证码图片处理对象
        # 根据图片左上角和右下角的坐标来截图
        # 小图x1=796 y1=116 x2=1043 y2=179
        # 大图x1=615 y1=183 x2=1032 y2=602

        image_small_offset = img.crop((796, 116, 1043, 179))
        image_small_offset.save("./Image_Small/" + image_name)
        image_big_offset = img.crop((615, 183, 1032, 602))
        image_big_offset.save("./Image_Big/" + image_name)

        # 图灵打码平台比对代码
        def b64_api(username, password, img_path, ID):
            with open(img_path, 'rb') as f:
                b64_data = base64.b64encode(f.read())
            b64 = b64_data.decode()
            data = {"username": username, "password": password, "ID": ID, "b64": b64, "version": "3.1.1"}
            data_json = json.dumps(data)
            result = json.loads(requests.post("http://www.fdyscloud.com.cn/tuling/predict", data=data_json).text)
            return result

        # 图像比对
        image_small_path = "./Image_Small/" + image_name
        result_small_offset = b64_api("lyylyy", 123456, image_small_path, "02156188")
        print(f"result_small_offset:{result_small_offset}")

        image_big_path = "./Image_Big/" + image_name
        result_big_offset = b64_api("lyylyy", 123456, image_big_path, "35484988")
        print(f"result_big_offset:{result_big_offset}")

        element = self.driver.find_element(By.CLASS_NAME, 'geetest_tip_content')
        result_small = result_small_offset["data"]["result"]

        # 定位到输入框
        # self.driver.find_element(By.CLASS_NAME).send_keys(result_small)

        print(result_small)
        for i in result_small:
            result = result_big_offset["data"][i]
            ActionChains(self.driver).move_to_element(element).move_by_offset(int(result["X坐标值"] - 110), result["Y坐标值"]).click().perform()
            time.sleep(1)

        click = self.driver.find_element(By.CLASS_NAME, 'geetest_commit_tip').click()
        time.sleep(1)
        self.driver.find_element(By.ID, 'btn').click()


if __name__ == '__main__':
    login = LoginData()
    login.chrome_settings()
    login.save_image()
